<template>
  <div>
    <!-- 查询 -->
    <div class="div0">
      <el-row :gutter="12">
        <el-col :span="7"
          >厂牌型号：<el-input
            v-model="V_model"
            placeholder="请输入内容"
            size="medium"
          ></el-input
        ></el-col>
        <el-col :span="7"
          >车牌号：<el-input
            v-model="V_licenseplate"
            placeholder="请输入内容"
            size="medium"
          ></el-input
        ></el-col>
        <el-col :span="7"
          >司机姓名：<el-input
            v-model="V_drivere"
            placeholder="请输入内容"
            size="medium"
          ></el-input
        ></el-col>
      </el-row>
      <el-row :gutter="12">
        <el-col :span="7"
          >所属公司：<el-input
            v-model="V_company"
            placeholder="请输入内容"
            size="medium"
          ></el-input
        ></el-col>

        <el-col :span="1.5"
          ><el-button type="primary" size="medium" @click="getVeha">查询</el-button></el-col
        >
      </el-row>

      <el-row :gutter="12">
        <el-col :span="1.5"
          ><el-button type="primary" size="medium" @click="OppenAdd"
            >新增</el-button
          ></el-col
        >
        <el-col :span="1"
          ><el-button type="primary" size="medium">删除</el-button></el-col
        >
      </el-row>
    </div>

    <!-- 分页显示 -->
    <div class="div1">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="V_model" label="厂牌型号" width="120">
        </el-table-column>
        <el-table-column prop="V_licenseplate" label="车牌号" width="120">
        </el-table-column>
        <el-table-column prop="V_drivere" label="司机姓名" width="120">
        </el-table-column>
        <el-table-column prop="V_company" label="所属公司" width="200">
        </el-table-column>
        <el-table-column prop="VLwh" label="车型(长*宽*高)" width="150">
        </el-table-column>
        <el-table-column prop="V_color" label="车辆颜色" width="120">
        </el-table-column>
        <el-table-column prop="V_titemb" label="购置日期" width="120">
        </el-table-column>
        <el-table-column prop="V_ynumber" label="运营证号" width="120">
        </el-table-column>
        <el-table-column prop="V_stimeb" label="保险到期时间" width="120">
        </el-table-column>
        <el-table-column prop="V_etimeb" label="年检到期时间" width="120">
        </el-table-column>
        <el-table-column prop="V_km" label="保养公里数" width="120">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="float: right; margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageindex"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagetotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 添加 -->
    <div>
      <el-dialog title="新增车辆" :visible.sync="dialogFormVisible">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="厂牌型号"
                :label-width="formLabelWidth"
                prop="V_model"
              >
                <el-input
                  v-model="ruleForm.V_model"
                  autocomplete="off"
                  placeholder="请输入厂牌型号"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="车牌号"
                :label-width="formLabelWidth"
                prop="V_licenseplate"
              >
                <el-input
                  v-model="ruleForm.V_licenseplate"
                  autocomplete="off"
                  placeholder="请输入车牌号"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="司机姓名"
                :label-width="formLabelWidth"
                prop="V_drivere"
              >
                <el-input
                  v-model="ruleForm.V_drivere"
                  autocomplete="off"
                  placeholder="请输入司机姓名"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="所属公司"
                :label-width="formLabelWidth"
                prop="V_company"
              >
                <el-input
                  v-model="ruleForm.V_company"
                  autocomplete="off"
                  placeholder="请输入所属公司"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="16">
            <el-col :span="4.5">
              <el-form-item
                label="车型"
                :label-width="formLabelWidth"
                prop="V_lon"
              >
                <el-input
                  v-model="ruleForm.V_lon"
                  autocomplete="off"
                  placeholder="长"
                  style="width: 90px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4.5">
              <el-form-item prop="V_width">
                <el-input
                  v-model="ruleForm.V_width"
                  autocomplete="off"
                  placeholder="宽"
                  style="width: 90px; margin-left: 2%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4.5">
              <el-form-item prop="V_hight">
                <el-input
                  v-model="ruleForm.V_hight"
                  autocomplete="off"
                  prop="V_hight"
                  placeholder="高"
                  style="width: 90px; margin-left: 2%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="车辆颜色"
                :label-width="formLabelWidth"
                prop="V_color"
              >
                <el-input
                  v-model="ruleForm.V_color"
                  autocomplete="off"
                  placeholder="请输入车辆颜色"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="购置日期"
                :label-width="formLabelWidth"
                prop="V_titem"
              >
                <el-input
                  v-model="ruleForm.V_titem"
                  type="date"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运营证号" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForm.V_ynumber"
                  autocomplete="off"
                  placeholder="请输入运营证号"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="保险到期日期" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForm.V_stime"
                  type="date"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年检到期日期" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForm.V_etime"
                  type="date"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="保养公里数设置"
                :label-width="formLabelWidth"
              >
                <el-input
                  v-model="ruleForm.V_km"
                  type="date"
                  autocomplete="off"
                  placeholder="请输入保养公里数"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getVeh } from "@/api/Vehicles";

export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      ruleForm: {},
      V_model: "",
      V_licenseplate: "",
      V_drivere: "",
      V_company: "",
      pageindex: 1,
      pagesize: 5,
      tableData: [],
      pagetotal: 0,

      rules: {
        V_model: [
          { required: true, message: "请输入厂牌型号", trigger: "change" },
        ],
        V_licenseplate: [
          { required: true, message: "请输入车牌号", trigger: "change" },
        ],
        V_lon: [{ required: true, message: "请输入长", trigger: "change" }],
        V_width: [{ required: true, message: "请输入宽", trigger: "change" }],
        V_hight: [{ required: true, message: "请输入高", trigger: "change" }],
        V_drivere: [
          { required: true, message: "请输入姓名", trigger: "change" },
        ],
        V_company: [
          { required: true, message: "请输入所属公司", trigger: "change" },
        ],
        V_color: [{ required: true, message: "请输入颜色", trigger: "change" }],
      },
    };
  },
  methods: {
    getVeha() {
      getVeh(
        this.pageindex,
        this.pagesize,
        this.V_model,
        this.V_licenseplate,
        this.V_drivere,
        this.V_company
      ).then((res) => {
        this.tableData = res.data.PageData;
        this.pagetotal = res.data.PageTotal;
      });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.getVeha();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageindex = val;
      this.getVeha();
      console.log(`当前页: ${val}`);
    },
    OppenAdd() {
      this.dialogFormVisible = true;
    },
    //添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
            if (valid) {
          this.$axios
            .post(
              "https://localhost:44360/api/app/t-mS_Vehicles/vehic",
              this.ruleForm
            )
            .then((res) => {
              if (res.data == 1) {
                alert("添加成功!");
                this.dialogFormVisible = false;
                this.getVeha();
              } else {
                alert("添加失败!");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除
    handleDelete(index, row) {
      if (confirm("确定删除此条数据吗?")) {
        this.$axios
          .delete(
            "https://localhost:44360/api/app/t-mS_Vehicles/vehicle/"+row.id                           
          )
          .then((res) => {
            if (res.data == 1) {
              alert("删除成功!");
              this.getVeha();
            }
          });
      }
    },
    resetForm() {
      this.ruleForm = {};
      this.dialogFormVisible = false;
    },
  },
  created() {
    this.getVeha();
  },
  computed: {},
};
</script>
<style  scoped>
.div0,
.div1 {
  margin-left: 1%;
  margin-top: 1%;
}
.el-row {
  margin-bottom: 20px;
}
.last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.el-input {
  width: 250px;
}
</style>
